<?php

namespace app\web\model;

use app\common\model\Companys;
use think\Model;

class CompanyWeb extends Model
{
    protected $table = 'sc_companys';

    public function getLogoAttr($value)
    {
        return $value ? env('API_HOST').$value : '';
    }

    public function getstuffAttr($value)
    {
        $value = explode(',', $value);
        foreach($value as $val) {
            if(isset(Companys::$stuffs[$val])) {
                $result[] = Companys::$stuffs[$val];
            }
        }
        return $result;

    }

    public function lists($type, $page_conf)
    {
        $where = [];
        if($type == 2) {
            $where[] = ['category_label', 'like', '%4%'];
        }
        $data = $this->field('id,xd_name,logo,bonus_total,hotspot,summary,bonus_one,bonus_two,bonus_three')
            ->where('status', Companys::XINDAI_ON)
            ->where($where)
            ->order('sort desc')
            ->paginate($page_conf);
        return $data;
    }

    public function info($id)
    {
        $data = [];
        $company = $this->where('id', $id)->where('status', Companys::XINDAI_ON)->find();
        if($company) {
            $item = [
                ['title' => '额度', 'content' => $company->loan_limit],
                ['title' => '利率', 'content' => $company->rate_quota],
                ['title' => '贷款期限', 'content' => $company->term_quota],
                ['title' => '还款方式', 'content' => $company->repayment],
                ['title' => '申请条件', 'content' => $company->apply],
                ['title' => '所需材料', 'content' => $company->stuff],
            ];
            $data = [
                'info' => [
                    'id' => $id,
                    'name' => $company->xd_name,
                    'logo'=>$company->logo,
                    'hotspot'=>$company->hotspot
                ],
                'item' => $item
            ];
        }
        return $data ?: '';
    }


}
